package com.cong.modules.news.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.cong.common.util.PageUtil;
import com.cong.common.util.StringUtils;
import com.cong.common.util.UUIDUtil;
import com.cong.common.vo.AjaxResultVo;
import com.cong.modules.base.BaseController;
import com.cong.modules.news.model.NewsCategory;
import com.cong.modules.news.service.INewsCategoryService;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 新闻分类表 前端控制器
 */
@Controller
@RequestMapping("/newsCategory")
public class NewsCategoryController extends BaseController {

	@Resource
	private INewsCategoryService newsCategoryService;

	/**
	 * 文章管理--文章类别
	 */
	@GetMapping("/toNewsCategoryPage")
	public String newsCategoryList() {
		return "newscategory/list";
	}

	/**
	 * 类别列表数据
	 *
	 * @param newsCategory 新闻分类
	 * @param limit 查询限制条数
	 * @param offset 查询游标值
	 * @return AjaxResultVo 列表参数
	 */
	@RequiresPermissions("newsCategory:list")
	@PostMapping("/list")
	@ResponseBody
	public AjaxResultVo pageCategory(NewsCategory newsCategory, Integer limit, Integer offset) {
		try {
			PageHelper.startPage(PageUtil.getPageNo(limit, offset), limit);
			List<NewsCategory> categoryList = newsCategoryService.listNewsCategory(newsCategory);
			PageInfo<NewsCategory> pageResult = new PageInfo<>(categoryList);
			AjaxResultVo resultVo = AjaxResultVo.success();
			resultVo.put("rows", pageResult.getList());
			resultVo.put("total", pageResult.getTotal());
			return resultVo;
		} catch (Exception e) {
			logger.error(String.format("文章分类列表查询异常：%s", e));
			return AjaxResultVo.error("查询失败");
		}
	}

	/**
	 * 跳转新增类别页面
	 */
	@RequiresPermissions("newsCategory:add")
	@GetMapping("/add")
	public String add() {
		return "newscategory/add";
	}

	/**
	 * 插入新增类别
	 *
	 * @param newsCategory 新闻分类
	 * @return AjaxResultVo 新增分类数据
	 */
	@RequiresPermissions("newsCategory:add")
	@PostMapping("/doAdd")
	@ResponseBody
	public AjaxResultVo addCategory(NewsCategory newsCategory) {
		try {
			if (StringUtils.isBlank(newsCategory.getCategoryName())) {
				return AjaxResultVo.error("类别名称不能为空！");
			}
			Date nowDate = new Date();
			newsCategory.setNewsCategoryId(Long.valueOf(UUIDUtil.getUniqueIdByUUId()));
			newsCategory.setCreateTime(nowDate);
			newsCategory.setUpdateTime(nowDate);
			if (newsCategoryService.save(newsCategory)) {
				return AjaxResultVo.success("添加类别成功");
			} else {
				return AjaxResultVo.error("添加类别失败");
			}
		} catch (Exception e) {
			logger.error(String.format("文章分类添加异常: %s", e));
			return AjaxResultVo.error("添加类别失败");
		}
	}

	/**
	 * 编辑文章类别详情
	 *
	 * @param model 页面跳转
	 * @param newsCategoryId 文章分类ID
	 * @return 跳转分页编辑页面
	 */
	@RequiresPermissions("newsCategory:edit")
	@GetMapping("/edit")
	public String detail(Model model, String newsCategoryId) {
		NewsCategory newsCategory = newsCategoryService.getById(newsCategoryId);
		model.addAttribute("newsCategory", newsCategory);
		return "newscategory/detail";
	}

	/**
	 * 编辑文章类别
	 *
	 * @param newsCategory 文章类别
	 * @return AjaxResultVo 新增文章实体
	 */
	@RequiresPermissions("newsCategory:edit")
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResultVo editCategory(NewsCategory newsCategory) {
		try {
			newsCategory.setUpdateTime(new Date());
			if (newsCategoryService.updateById(newsCategory)) {
				return AjaxResultVo.success("编辑类别成功");
			} else {
				return AjaxResultVo.error("编辑类别失败");
			}
		} catch (Exception e) {
			logger.error(String.format("文章分类编辑异常 NewsCategoryController.editCategory%s", e));
			return AjaxResultVo.error("编辑类别失败");
		}
	}

	/**
	 * 删除文章类别
	 *
	 * @param newsCategoryId 文章类别ID
	 * @return 删除文章类别结果
	 */
	@RequiresPermissions("newsCategory:delete")
	@PostMapping("/delete")
	@ResponseBody
	public AjaxResultVo deleteCategory(String newsCategoryId) {
		try {
			if (StringUtils.isEmpty(newsCategoryId)) {
				return AjaxResultVo.error("删除失败,未选择删除选项");
			}
			if (newsCategoryService.removeById(newsCategoryId)) {
				return AjaxResultVo.success("删除成功");
			}
			return AjaxResultVo.error("删除失败");
		} catch (Exception e) {
			logger.error(String.format("文章分类删除异常 NewsCategoryController.deleteCategory%s", e));
			return AjaxResultVo.error("删除失败");
		}
	}

}
